package com.jeff.controller;

import com.jeff.common.BaseInfo;
import com.jeff.common.mConst;
import com.jeff.po.Activity;
import com.jeff.service.ActivityService;
import com.jeff.util.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("activity")
public class ActivityController {
    @InitBinder
    public void intDate(ServletRequestDataBinder binder) {
        System.out.println("执行了InitBinder方法");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
    }

    @Autowired
    public ActivityService activityService;


    @RequestMapping("add")
    @ResponseBody
    public ResultMap add(Activity activity, HttpServletRequest request) {
        ResultMap rm = new ResultMap();
        try {
            activity.setState(mConst.STATE_T);
            activity.setApproval(mConst.STATE_H);
            activity.setCreateDept(BaseInfo.GetUserRootDeptId(request));
            activityService.add(activity);
            rm.success();
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("增加失败"+e.getMessage());
        }
        return rm;
    }

    @RequestMapping("page")
    @ResponseBody
    public ResultMap page(Activity activity, HttpServletRequest request) {
        ResultMap rm = new ResultMap();
        try {
            activity.setId(null);
            String deptId = BaseInfo.GetUserRootDeptId(request);
            activity.setCreateDept(deptId);
            rm.success().page(activityService.findByPage(activity));
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }

}
